package yangjiaru;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

import java.util.*;

public class DoubleBall {


    public static void main(String[] args) {
        int[] winningNumbers = generateWinningNumbers();
        int[] userNumbers = getUserInputNumbers();


        System.out.println("中奖号码为：");
        printNumbers(winningNumbers);
        System.out.println("您投注号码为：");
        printNumbers(userNumbers);

        int redBallCount = countMatchedRedBalls(winningNumbers, userNumbers);
        int blueBallCount = (winningNumbers[6] == userNumbers[6]) ? 1 : 0;

        System.out.println("您命中了" + redBallCount + "个红色球号码，" + blueBallCount + "个蓝色球号码。");
        String awardLevel = determineAwardLevel(redBallCount, blueBallCount);
        System.out.println(awardLevel);
    }

    public static int[] generateWinningNumbers() {
        Random random = new Random();
        Set<Integer> redBalls = new HashSet<>();
        while (redBalls.size() < 6) {
            redBalls.add(random.nextInt(33) + 1);
        }
        int blueBall = random.nextInt(16) + 1;

        int[] numbers = new int[7];
        int index = 0;
        for (int redBall : redBalls) {
            numbers[index++] = redBall;
        }
        numbers[6] = blueBall;
        return numbers;
    }

    public static int[] getUserInputNumbers() {
        Scanner scanner = new Scanner(System.in);
        Set<Integer> redBalls = new HashSet<>();
        int[] numbers = new int[7];

        System.out.println("请输入红球号码，输入任意一个范围1-33的数字：");
        for (int i = 0; i < 6; i++) {
            while (true) {
                int num = scanner.nextInt();
                if (num >= 1 && num <= 33 && !redBalls.contains(num)) {
                    redBalls.add(num);
                    numbers[i] = num;
                    break;
                } else {
                    System.out.println("输入有误，请重新输入一个1-33范围内且不重复的数字：");
                }
            }
        }

        System.out.println("请输入蓝球号码，输入任意一个范围1-16的数字：");
        while (true) {
            int num = scanner.nextInt();
            if (num >= 1 && num <= 16) {
                numbers[6] = num;
                break;
            } else {
                System.out.println("输入有误，请重新输入一个1-16范围内的数字：");
            }
        }

        scanner.close();
        return numbers;
    }

    public static int countMatchedRedBalls(int[] winningNumbers, int[] userNumbers) {
        int count = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winningNumbers[i] == userNumbers[j]) {
                    count++;
                }
            }
        }
        return count;
    }

    public static String determineAwardLevel(int redBallCount, int blueBallCount) {
        if (redBallCount == 6 && blueBallCount == 1) {
            return "恭喜您，中了一等奖！";
        } else if (redBallCount == 6 && blueBallCount == 0) {
            return "恭喜您，中了二等奖！";
        } else if (redBallCount == 5 && blueBallCount == 1) {
            return "恭喜您，中了三等奖！";
        } else if (redBallCount == 5 && blueBallCount == 0 || redBallCount == 4 && blueBallCount == 1) {
            return "恭喜您，中了四等奖！";
        } else if (redBallCount == 4 && blueBallCount == 0 || redBallCount == 3 && blueBallCount == 1) {
            return "恭喜您，中了五等奖！";
        } else if (redBallCount == 2 && blueBallCount == 1 || redBallCount == 1 && blueBallCount == 1 || redBallCount == 0 && blueBallCount == 1) {
            return "恭喜您，中了六等奖！";
        } else {
            return "很遗憾，您未中奖！";
        }
    }

    public static void printNumbers(int[] numbers) {
        List<Integer> list = new ArrayList<>();
        for (int num : numbers) {
            list.add(num);
        }
        System.out.println(list);
    }
}
